package com.jiangruan.javaproject.service;
/**
 * @author:xulonghua
 * @email:3162193565@qq.com desc:
 * datatime 2021/12/13 19:59
 */

import com.alibaba.fastjson.JSONObject;
import com.jiangruan.javaproject.Constants;
import com.jiangruan.javaproject.request.HttpUtils;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 *@author:xulonghua
 *@email:3162193565@qq.com
 *desc：该方法用于rpc与bitcoin客户端的调用
 *datetime 2021/12/13 19:59
 */
public class UtilCommon {
    public static String utilcommon(int id,String method,Object... params){
        String auth= Constants.BITCOIN_PRC_USER+":"+Constants.BITCOIN_RPC_PWD;
        //base64编码
        Base64.Encoder encoder=Base64.getEncoder ();
        String authBase64 =encoder.encodeToString (auth.getBytes () );
        //准备请求头
        Map<String,String> header =new HashMap<> ();
        header.put ( "ContentType","application/json" );
        header.put ( "Authorization","Basic "+authBase64);
        //准备请求数据
        HashMap<String,Object> body=new HashMap<> ();
        body.put ( "id",id);
        body.put ( "method",method );
        body.put ( "params",params );
        body.put ( "json_rpc",Constants.RPC_VERSION );
        byte[]  bodyBytes= JSONObject.toJSONBytes ( body );
        String result = HttpUtils.post ( Constants.BITCOIN_PRC_URL,header,bodyBytes );
        //System.out.println (result);
        return result;
    }
}
